package com.darren.euc.util;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * Create Date: 2017年09月04日
 * 类功能描述：自定义Property处理配置类
 * 主要用来把spring上下文中properties键值放入Map，供普通类调用
 */
public class PropertyConfigurer extends PropertyPlaceholderConfigurer {

    private static Map<String, Object> ctxPropertiesMap;

    @Override
    protected void processProperties(ConfigurableListableBeanFactory beanFactory,
                                     Properties props)throws BeansException {
        super.processProperties(beanFactory, props);
        //load properties to ctxPropertiesMap
        ctxPropertiesMap = new HashMap<String, Object>();
        for (Object key : props.keySet()) {
            String keyStr = key.toString();
            String value = props.getProperty(keyStr);
            ctxPropertiesMap.put(keyStr, value);
        }
    }

    /**
     * PropertiesHandle 会用到
     * @param name
     * @return
     */
    public static Object getContextProperty(String name) {
        return ctxPropertiesMap.get(name);
    }

    /**
     * 获取properties值
     * @return
     */
    public static String getPropertiesStr(String propertyKey){
        String value = (String) getContextProperty(propertyKey);
        if(value!=null){
            return value;
        }
        return null;
    }
}
